package com.tedu.jdbc;

import java.sql.*;

/**
 * Author：毕龙
 * Date: 2023/7/21 9:12
 */
public class TestJDBCConnection4 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取链接  java访问数据库的一个协议
        // 协议  jdbc:mysql://
        //数据库的ip  localhost:
        //数据库的端口号  3306
        //jdbc:oracle:thin:@localhost:1521/db4
        String url = "jdbc:mysql://localhost:3306/db4";
        //数据库的用户名
        String user = "root";
        //数据用户密码  不一定是root
        String password = "root";
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println(conn);
        //3.执行sql
        String sql = "select * from user where id = 4";
        PreparedStatement pstm = conn.prepareStatement(sql);
        ResultSet rs = pstm.executeQuery();

        //4.处理查询结果
        if(rs.next()){
            //获取每一个字段的值
            int id = rs.getInt(1);
            //int id = rs.getInt("id");
            String name = rs.getString(2);
            String password1 = rs.getString(3);
            Date birthday = rs.getDate(4);
            Date createTime = rs.getDate(5);
            User user1 = new User(id, name, password1, birthday, createTime);
            System.out.println(user1);

        }

        //5释放资源
        rs.close();
        pstm.close();
        conn.close();

    }
}
